<template>
  <UserLayout :active-index="activeIndex" :active-menu="activeMenu">
    <div class="settings-content">
      <!-- 页面标题 -->
      <div class="settings-header">
        <h2>设置中心</h2>
        <p>管理您的账户设置和偏好</p>
      </div>

      <!-- 设置内容 -->
      <div class="settings-main">
        <!-- 侧边栏导航 -->
        <div class="settings-sidebar">
          <el-menu :default-active="activeTab" @select="switchTab" class="settings-menu">
            <el-menu-item index="account">
              <el-icon><User /></el-icon>
              <span>账号设置</span>
            </el-menu-item>
            <el-menu-item index="notification">
              <el-icon><Bell /></el-icon>
              <span>通知设置</span>
            </el-menu-item>
            <el-menu-item index="privacy">
              <el-icon><Lock /></el-icon>
              <span>隐私设置</span>
            </el-menu-item>
            <el-menu-item index="display">
              <el-icon><Monitor /></el-icon>
              <span>显示设置</span>
            </el-menu-item>
            <el-menu-item index="security">
              <el-icon><Key /></el-icon>
              <span>安全设置</span>
            </el-menu-item>
            <el-menu-item index="about">
              <el-icon><InfoFilled /></el-icon>
              <span>关于系统</span>
            </el-menu-item>
          </el-menu>
        </div>

        <!-- 设置内容区域 -->
        <div class="settings-content-area">
          <!-- 账号设置 -->
          <div v-show="activeTab === 'account'" class="settings-tab">
            <div class="tab-header">
              <h3>账号设置</h3>
              <p>管理您的账号信息</p>
            </div>
            <div class="tab-content">
              <el-form :model="accountForm" label-width="120px" class="settings-form">
                <el-form-item label="用户名">
                  <el-input v-model="accountForm.username" />
                </el-form-item>

                <el-form-item label="邮箱">
                  <el-input v-model="accountForm.email" />
                </el-form-item>

                <el-form-item label="手机号">
                  <el-input v-model="accountForm.phone" />
                </el-form-item>

                <el-form-item label="真实姓名">
                  <el-input v-model="accountForm.realName" />
                </el-form-item>

                <el-form-item label="性别">
                  <el-radio-group v-model="accountForm.gender">
                    <el-radio label="male">男</el-radio>
                    <el-radio label="female">女</el-radio>
                    <el-radio label="other">其他</el-radio>
                  </el-radio-group>
                </el-form-item>

                <el-form-item label="出生日期">
                  <el-date-picker
                      v-model="accountForm.birthDate"
                      type="date"
                      placeholder="选择日期"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                  />
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="saveAccountSettings">保存设置</el-button>
                  <el-button @click="resetAccountForm">重置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>

          <!-- 通知设置 -->
          <div v-show="activeTab === 'notification'" class="settings-tab">
            <div class="tab-header">
              <h2>通知设置</h2>
              <p>管理您的通知偏好</p>
            </div>

            <div class="settings-content">
              <el-form :model="notificationForm" label-width="150px" class="settings-form">
                <el-form-item label="接收邮件通知">
                  <el-switch v-model="notificationForm.emailNotifications" />
                </el-form-item>

                <el-form-item label="接收短信通知">
                  <el-switch v-model="notificationForm.smsNotifications" />
                </el-form-item>

                <el-form-item label="接收系统通知">
                  <el-switch v-model="notificationForm.systemNotifications" />
                </el-form-item>

                <el-divider>消息类型设置</el-divider>

                <el-form-item label="房源相关通知">
                  <el-switch v-model="notificationForm.houseNotifications" />
                </el-form-item>

                <el-form-item label="合同相关通知">
                  <el-switch v-model="notificationForm.contractNotifications" />
                </el-form-item>

                <el-form-item label="维修相关通知">
                  <el-switch v-model="notificationForm.maintenanceNotifications" />
                </el-form-item>

                <el-form-item label="预约相关通知">
                  <el-switch v-model="notificationForm.appointmentNotifications" />
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="saveNotificationSettings">保存设置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>

          <!-- 隐私设置 -->
          <div v-show="activeTab === 'privacy'" class="settings-tab">
            <div class="tab-header">
              <h2>隐私设置</h2>
              <p>管理您的隐私偏好</p>
            </div>

            <div class="settings-content">
              <el-form :model="privacyForm" label-width="200px" class="settings-form">
                <el-form-item label="个人信息可见性">
                  <el-select v-model="privacyForm.profileVisibility" placeholder="请选择">
                    <el-option label="所有人可见" value="public" />
                    <el-option label="仅自己可见" value="private" />
                    <el-option label="仅房东/中介可见" value="landlord-agent" />
                    <el-option label="仅认证用户可见" value="authenticated" />
                  </el-select>
                </el-form-item>

                <el-form-item label="联系方式可见性">
                  <el-select v-model="privacyForm.contactVisibility" placeholder="请选择">
                    <el-option label="所有人可见" value="public" />
                    <el-option label="仅自己可见" value="private" />
                    <el-option label="仅房东/中介可见" value="landlord-agent" />
                    <el-option label="仅认证用户可见" value="authenticated" />
                  </el-select>
                </el-form-item>

                <el-form-item label="房源浏览记录">
                  <el-switch v-model="privacyForm.saveBrowsingHistory" />
                </el-form-item>

                <el-form-item label="搜索历史记录">
                  <el-switch v-model="privacyForm.saveSearchHistory" />
                </el-form-item>

                <el-form-item label="个性化推荐">
                  <el-switch v-model="privacyForm.personalizedRecommendations" />
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="savePrivacySettings">保存设置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>

          <!-- 显示设置 -->
          <div v-show="activeTab === 'display'" class="settings-tab">
            <div class="tab-header">
              <h2>显示设置</h2>
              <p>自定义界面显示偏好</p>
            </div>

            <div class="settings-content">
              <el-form :model="displayForm" label-width="150px" class="settings-form">
                <el-form-item label="主题">
                  <el-select v-model="displayForm.theme" placeholder="请选择主题">
                    <el-option label="浅色主题" value="light" />
                    <el-option label="深色主题" value="dark" />
                    <el-option label="自动" value="auto" />
                  </el-select>
                </el-form-item>

                <el-form-item label="语言">
                  <el-select v-model="displayForm.language" placeholder="请选择语言">
                    <el-option label="简体中文" value="zh-CN" />
                    <el-option label="English" value="en" />
                  </el-select>
                </el-form-item>

                <el-form-item label="字体大小">
                  <el-slider
                      v-model="displayForm.fontSize"
                      :min="12"
                      :max="20"
                      show-input
                      input-size="small"
                  />
                </el-form-item>

                <el-form-item label="默认房源视图">
                  <el-radio-group v-model="displayForm.defaultHouseView">
                    <el-radio label="grid">网格视图</el-radio>
                    <el-radio label="list">列表视图</el-radio>
                  </el-radio-group>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="saveDisplaySettings">保存设置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>

          <!-- 安全设置 -->
          <div v-show="activeTab === 'security'" class="settings-tab">
            <div class="tab-header">
              <h2>安全设置</h2>
              <p>管理您的账户安全</p>
            </div>

            <div class="settings-content">
              <el-form :model="securityForm" label-width="150px" class="settings-form">
                <el-form-item label="修改密码">
                  <el-button type="primary" @click="showChangePasswordDialog = true">修改密码</el-button>
                </el-form-item>

                <el-form-item label="双重验证">
                  <el-switch v-model="securityForm.twoFactorAuth" />
                </el-form-item>

                <el-form-item label="登录设备管理">
                  <el-button @click="showDevicesDialog = true">查看设备</el-button>
                </el-form-item>

                <el-form-item label="登录历史">
                  <el-button @click="showLoginHistoryDialog = true">查看历史</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>

          <!-- 关于系统 -->
          <div v-show="activeTab === 'about'" class="settings-tab">
            <div class="tab-header">
              <h2>关于系统</h2>
              <p>系统信息和版本</p>
            </div>

            <div class="settings-content">
              <div class="about-content">
                <div class="logo">
                  <img src="@/assets/logo.png" alt="HousingHub Logo" v-if="false">
                  <div class="logo-placeholder">HH</div>
                </div>

                <div class="system-info">
                  <el-descriptions :column="1" border>
                    <el-descriptions-item label="系统名称">HousingHub 房源管理系统</el-descriptions-item>
                    <el-descriptions-item label="版本号">v1.0.0</el-descriptions-item>
                    <el-descriptions-item label="构建时间">2025-08-04</el-descriptions-item>
                    <el-descriptions-item label="技术支持">housinghub@example.com</el-descriptions-item>
                    <el-descriptions-item label="官方网站">
                      <el-link type="primary" href="https://housinghub.example.com" target="_blank">
                        https://housinghub.example.com
                      </el-link>
                    </el-descriptions-item>
                  </el-descriptions>

                  <div class="copyright">
                    <p>© 2025 HousingHub. 保留所有权利。</p>
                    <p>本系统仅供学习和实习使用，不用于商业用途。</p>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 修改密码对话框 -->
    <el-dialog
        v-model="showChangePasswordDialog"
        title="修改密码"
        width="500px"
        :before-close="handleClose"
    >
      <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="100px">
        <el-form-item label="当前密码" prop="currentPassword">
          <el-input v-model="passwordForm.currentPassword" type="password" show-password />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword" type="password" show-password />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword" type="password" show-password />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showChangePasswordDialog = false">取消</el-button>
          <el-button type="primary" @click="changePassword">确认修改</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 登录设备对话框 -->
    <el-dialog
        v-model="showDevicesDialog"
        title="登录设备"
        width="600px"
    >
      <el-table :data="loginDevices" style="width: 100%">
        <el-table-column prop="device" label="设备" />
        <el-table-column prop="location" label="登录地点" />
        <el-table-column prop="time" label="登录时间" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" type="danger" @click="removeDevice(scope.row)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDevicesDialog = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 登录历史对话框 -->
    <el-dialog
        v-model="showLoginHistoryDialog"
        title="登录历史"
        width="600px"
    >
      <el-table :data="loginHistory" style="width: 100%">
        <el-table-column prop="time" label="登录时间" />
        <el-table-column prop="ip" label="IP地址" />
        <el-table-column prop="location" label="登录地点" />
        <el-table-column prop="status" label="状态" />
      </el-table>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showLoginHistoryDialog = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </UserLayout>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import UserLayout from '@/components/layout/UserLayout.vue'
import {
  User,
  Bell,
  Lock,
  Monitor,
  Key,
  InfoFilled,
  House,
  Search,
  OfficeBuilding,
  ChatLineSquare,
  Setting,
  ArrowLeft
} from '@element-plus/icons-vue'

const router = useRouter()
const activeMenu = ref('settings')
const activeIndex = ref('4')
const activeTab = ref('account')

// 表单引用
const passwordFormRef = ref()

// 表单数据
const accountForm = reactive({
  username: '张三',
  email: 'zhangsan@example.com',
  phone: '13800138000',
  realName: '张三',
  gender: 'male',
  birthDate: '1990-01-01'
})

const notificationForm = reactive({
  emailNotifications: true,
  smsNotifications: true,
  systemNotifications: true,
  houseNotifications: true,
  contractNotifications: true,
  maintenanceNotifications: true,
  appointmentNotifications: true
})

const privacyForm = reactive({
  profileVisibility: 'public',
  contactVisibility: 'authenticated',
  saveBrowsingHistory: true,
  saveSearchHistory: true,
  personalizedRecommendations: true
})

const displayForm = reactive({
  theme: 'light',
  language: 'zh-CN',
  fontSize: 14,
  defaultHouseView: 'grid'
})

const securityForm = reactive({
  twoFactorAuth: false
})

const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 对话框显示状态
const showChangePasswordDialog = ref(false)
const showDevicesDialog = ref(false)
const showLoginHistoryDialog = ref(false)

// 模拟数据
const loginDevices = ref([
  { device: 'Windows 10 / Chrome', location: '北京', time: '2025-08-03 14:30:22' },
  { device: 'iPhone 12 / Safari', location: '上海', time: '2025-08-02 09:15:45' },
  { device: 'MacBook Pro / Firefox', location: '广州', time: '2025-08-01 16:22:10' }
])

const loginHistory = ref([
  { time: '2025-08-03 14:30:22', ip: '192.168.1.100', location: '北京', status: '成功' },
  { time: '2025-08-03 08:45:10', ip: '192.168.1.101', location: '北京', status: '成功' },
  { time: '2025-08-02 09:15:45', ip: '203.123.45.67', location: '上海', status: '成功' },
  { time: '2025-08-01 16:22:10', ip: '113.56.78.90', location: '广州', status: '成功' }
])

// 密码验证规则
const passwordRules = {
  currentPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ]
}

// 自定义密码确认验证
function validatePassword(rule, value, callback) {
  if (value !== passwordForm.newPassword) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 切换标签页
const switchTab = (tab) => {
  activeTab.value = tab
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 顶部导航栏跳转
const goToHome = () => {
  router.push('/')
}

const goToSearch = () => {
  router.push('/house/search')
}

const goToPublish = () => {
  router.push('/house/publish')
}

const goToMessages = () => {
  router.push('/message')
}

// 用户命令处理
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      ElMessage.success('退出登录成功')
      localStorage.removeItem('userEmail')
      localStorage.removeItem('isLoggedIn')
      router.push('/login')
      break
  }
}

// 菜单选择处理
const handleSelect = (key) => {
  switch (key) {
    case '1':
      router.push('/')
      break
    case '2':
      router.push('/house/search')
      break
    case '3':
      router.push('/house/publish')
      break
    case '4':
      router.push('/message')
      break
  }
}

// 保存设置
const saveAccountSettings = () => {
  ElMessage.success('账号设置已保存')
}

const saveNotificationSettings = () => {
  ElMessage.success('通知设置已保存')
}

const savePrivacySettings = () => {
  ElMessage.success('隐私设置已保存')
}

const saveDisplaySettings = () => {
  ElMessage.success('显示设置已保存')
}

// 重置表单
const resetAccountForm = () => {
  accountForm.username = '张三'
  accountForm.email = 'zhangsan@example.com'
  accountForm.phone = '13800138000'
  accountForm.realName = '张三'
  accountForm.gender = 'male'
  accountForm.birthDate = '1990-01-01'
  ElMessage.info('表单已重置')
}

// 修改密码
const changePassword = () => {
  passwordFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('密码修改成功')
      showChangePasswordDialog.value = false
      // 重置表单
      passwordForm.currentPassword = ''
      passwordForm.newPassword = ''
      passwordForm.confirmPassword = ''
    } else {
      ElMessage.error('请检查表单填写是否正确')
      return false
    }
  })
}

// 移除设备
const removeDevice = (device) => {
  ElMessageBox.confirm(`确定要移除 ${device.device} 这个设备吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('设备已移除')
  })
}

// 关闭对话框前的处理
const handleClose = (done) => {
  ElMessageBox.confirm('确认关闭修改密码页面吗？')
      .then(() => {
        done()
      })
      .catch(() => {
        // 取消关闭
      })
}

// 页面加载时的处理
onMounted(() => {
  // 可以在这里获取用户设置信息
  console.log('加载设置页面')
})
</script>

<style scoped>
.settings-content {
  padding: 20px;
}

.settings-header {
  margin-bottom: 30px;
}

.settings-header h2 {
  color: #2c3e50;
  font-size: 28px;
  margin: 0 0 10px;
}

.settings-header p {
  color: #7f8c8d;
  font-size: 16px;
  margin: 0;
}

.settings-main {
  display: flex;
  gap: 30px;
}

.settings-sidebar {
  width: 250px;
  flex-shrink: 0;
}

.settings-menu {
  border: none;
  background: #f8f9fa;
  border-radius: 8px;
}

.settings-content-area {
  flex: 1;
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 设置标签页样式 */

.shape-5 {
  width: 250px;
  height: 250px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-duration: 22s;
  background: radial-gradient(circle, rgba(155, 89, 182, 0.2) 0%, transparent 70%);
}

.shape-6 {
  width: 180px;
  height: 180px;
  top: 70%;
  left: 10%;
  animation-duration: 28s;
  background: radial-gradient(circle, rgba(231, 76, 60, 0.2) 0%, transparent 70%);
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(20px, 20px) rotate(90deg);
  }
  50% {
    transform: translate(0, 40px) rotate(180deg);
  }
  75% {
    transform: translate(-20px, 20px) rotate(270deg);
  }
  100% {
    transform: translate(0, 0) rotate(360deg);
  }
}

.settings-wrapper {
  width: 100%;
  height: 100vh;
  z-index: 1;
  display: flex;
  flex-direction: column;
}

.top-navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.navbar-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.navbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.navbar-right {
  display: flex;
  align-items: center;
}

.app-title {
  color: white;
  font-size: 24px;
  font-weight: 700;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: white;
}

.username {
  font-size: 16px;
  font-weight: 500;
}

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 250px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  padding: 20px 0;
  height: 100%;
  box-shadow: 3px 0 10px rgba(0, 0, 0, 0.2);
}

.el-menu-vertical {
  border: none !important;
  height: 100%;
}

.el-menu-item {
  margin: 5px 10px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
}

.content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  overflow-y: auto;
  padding: 20px;
}

.settings-tab {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.tab-header {
  margin-bottom: 30px;
}

.tab-header h2 {
  color: #2c3e50;
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
}

.tab-header p {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0;
}

.settings-content {
  flex: 1;
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
}

.settings-form {
  max-width: 600px;
}

.settings-form .el-form-item {
  margin-bottom: 25px;
}

.about-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.logo {
  margin-bottom: 30px;
}

.logo-placeholder {
  width: 100px;
  height: 100px;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  font-weight: bold;
  color: white;
  margin: 0 auto;
}

.system-info {
  width: 100%;
  max-width: 600px;
}

.copyright {
  margin-top: 30px;
  color: #7f8c8d;
  font-size: 14px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

@media (max-width: 992px) {
  .sidebar {
    width: 80px;
  }

  .app-title,
  .el-menu-item span {
    display: none;
  }

  .navbar-center {
    display: none;
  }

  .settings-content {
    padding: 20px;
  }
}
</style>
